package com.soul.common.handler;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 插入时 自动填充字段
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        Long userId = StpUtil.isLogin() ? StpUtil.getLoginIdAsLong() : 100L;
        this.strictInsertFill(metaObject, "createTime",  LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "createUser",  Long.class, userId);
        this.strictInsertFill(metaObject, "updateUser", Long.class, userId);
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }
    /**
     * 更新时 自动填充字段
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        Long userId = StpUtil.isLogin() ? StpUtil.getLoginIdAsLong() : 100L;
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateUser", Long.class, userId);
    }
}
